﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl a wvr" xmlns:movi="http://ns.woodbase.dk/Movies.xsd" xmlns:wvr="http://schemas.datacontract.org/2004/07/Woodbase.CodeBase.WVR.Objects"  xmlns:a="http://schemas.datacontract.org/2004/07/WVR.Services"
>
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/ArrayOfMovie">
    <!-- SELECT TOP 3 AND ORDER THEM DESCENDING -->
    <xsl:apply-templates select="//Movie[position() &gt; count(//Movie)-3]">
      <xsl:with-param name="enableDragging" select="'false'" />
      <xsl:with-param name="rented" select="@rented" />
      <xsl:sort select="position()" order="descending" />
    </xsl:apply-templates>
  </xsl:template>

  <!-- List all movies -->
  <xsl:template match="/ArrayOfMovie[@type='normal']">
    <xsl:apply-templates select="//Movie">
      <xsl:with-param name="enableDragging" select="'true'" />
      <xsl:with-param name="rented" select="@rented" />
      <xsl:sort select="@title" order="ascending" />
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="Movie">
    <xsl:param name="enableDragging" />
    <xsl:param name="rented" />
    <xsl:variable name="draggable">
      <xsl:choose>
        <xsl:when test="$enableDragging = 'true'">movie-ui-draggable</xsl:when>
        <xsl:otherwise>
          movie-non-draggable
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <div class="movieBlock {normalize-space($draggable)}">
      <h2>
        <xsl:value-of select="@title"/>
      </h2>
      <div class="left">
        <xsl:if test="@runtime &gt; 0">
          Runtime: <xsl:value-of select="@runtime"/>min.
        </xsl:if>
        <div>
          <xsl:if test="@production-year">
            Production year: <xsl:value-of select="@production-year"/>
          </xsl:if>
        </div>
        <xsl:if test="movi:genres">
          <div class="genre genres-target-{position()}{$rented}">
            <xsl:if test="count(movi:genres) &gt; 0">
              <xsl:attribute name="title">
                Genre:
                <xsl:for-each select="movi:genres[position() &gt; 0]/@name">
                  <xsl:text> - </xsl:text>
                  <xsl:value-of select="." />
                </xsl:for-each>
              </xsl:attribute>
            </xsl:if>
            <xsl:value-of select="movi:genres[position()=1]/@name"/>
          </div>
        </xsl:if>
      </div>
      <div class="right thumb">
        <xsl:choose>
          <xsl:when test="@front-cover != ''">
            <img src="{@front-cover}" alt="" width="50" />
          </xsl:when>
          <xsl:otherwise>
            <img src="/Images/Movies.png" alt="No image" width="50"  />
          </xsl:otherwise>
        </xsl:choose>
      </div>
    </div>

    <script type="text/javascript">
      $(document).ready(function(){
      $(".genres-target-<xsl:value-of select="position()" /><xsl:value-of select="$rented" />").tooltip({
      track: true,
      delay: 0,
      showURL: false,
      showBody: " - ",
      fade: 250,
      extraClass: "movieTip"
      });
      });
    </script>

  </xsl:template>
</xsl:stylesheet>
